<?php

namespace app\service;

use app\message;
use app\model\OrderDeal;
use app\model\OrderEnterpriseExchange;
use app\model\SystemBusiness;
use app\model\UserOrganization;
use app\stateData;
use ReflectionClass;
use think\facade\Db;

class Clue
{

    //根据条件获取select option
    public function getOption($data)
    {
        $org = new UserOrganization();
        $user = new \app\model\User();
        $system = new System();
        $binsess = new SystemBusiness();
        $certificateSer = new Certificate();
        $results = [];
        if ($data['option'] == 1) {

            //查询数据库文件
//            if (!isset($data['where'])) return false;

            if ($data['option_data'] === 'user') //员工组织架构返回
            {
                $orgModel = $org->getStructureArr ();
                $results = $system->getOrgTypeUserList($orgModel);
            }elseif ($data['option_data'] === 'system_business'){
                $result = Db::table($data['option_data'])->group('business_name')->field('business_name as id,business_name as title')->select()->toArray ();
//            print_r ($result);die;
                $results = $system->menuTitleTerr($result,$binsess);
            } elseif ($data['option_data'] === 'system_businesss') {
                $result = Db::table('system_business')->select()->toArray();

                foreach ($result as $key => $item) {
                    if (!empty($item['business_category'])){
                        $results[$key]['id'] = $item['id'];
                        $results[$key]['text'] = $item['business_category'];
                    }
                }
            }elseif($data['option_data'] === 'system_china'){

                $result = Db::table('system_china')->field('code_id as id,city_name as title,parent_id')->select()->toArray ();
                $results = Tree($result, 'id', 'parent_id');

            } elseif($data['option_data'] === 'user_organization'){

                $result = Db::table($data['option_data'])->select()->toArray ();

                $results = $system->menuTreeList($result);
            }else if($data['option_data'] === 'system_certificate'){ //证书查询
                $result = Db::table($data['option_data'])->whereRaw($data['where'])->order('parent_id ASC')->select()->toArray ();

                $pid = '';
                foreach ($result as $key => $item) {
                    if (isset($item['id']) && isset($item['certificate_name'])){
                        if ($certificateSer->getParentName ($item['id'])){
                            $results[$key]['id'] = $item['id'];
                            //查询该证书是否有父级
                            $results[$key]['text'] = $certificateSer->getParentName ($item['id']);
                        }
                    }
                }
            }else{
                $result = Db::table($data['option_data'])->whereRaw($data['where'])->select()->toArray ();

                foreach ($result as $key => $item) {
                    if (isset($item['id']) && isset($item['certificate_name'])){
                        $results[$key]['id'] = $item['id'];
                        $results[$key]['text'] = $item['certificate_name'];
                    }
                }
            }
        } else {

            $ReflectionClass = new ReflectionClass(stateData::class);

            $result = $ReflectionClass->getStaticPropertyValue($data['option_data']);

            foreach ($result as $key => $item) {
                //删除待付款
                if ($item == '待付款'){
                    continue;
                }
//                if ($item == '无项目' || $item == '不交' || $item == '无B证' || $item == '录单中'){
                if ($item == '无项目' || $item == '不交' || $item == '无B证' ){
                    $results[$key]['id'] = 999;
                    $results[$key]['text'] = $item;
                }else{
                    if ($key != 0 || $item == '待审核'){
                        $results[$key]['id'] = $key;
                        $results[$key]['text'] = $item;
                    }
                }


            }
        }
        return $results;
    }

    //人才证书表格数据处理
    public function personData($data, $uid, $type = 1)
    {

        if (empty($data)) {
            return [];
        }
        //如果是一维数组 强制转化为二维数组
        if (is_one_dimensional_array ($data)) {

            $re = $data;
            $data = [$re];
        }
        $cer = new Certificate();
        $deal = new OrderDeal();
        $dzModel = new OrderEnterpriseExchange();
        foreach ($data as &$datum) {
            //获取联系方式
            if (empty($datum['mobile'])){
                $datum['mobile'] = Db::table('clue_mobile')->where(['clue_p_id' => $datum['id'],'type'=>$type])->order('create_date DESC')->value('mobile');
            }
            //其他证书
            $cerArr = Db::table('clue_person_certificate')
//                ->fetchSql (true)
                ->where(['clue_p_id' => $datum['id']])
                ->order('create_date DESC')
                ->field ('id,certificate_leixing,certificate_leibie,certificate_dengji,certificate_zhuanye')
                ->select();

            $str = '';
            foreach ($cerArr as $item) {
                if ($item['certificate_leixing']){
                    $str .= Db::table('system_certificate')->where (['id'=>$item['certificate_leixing']])->value ('certificate_name').'/'?:'';
                }
                if ($item['certificate_leibie']){
                    $str .= Db::table('system_certificate')->where (['id'=>$item['certificate_leibie']])->value ('certificate_name').'/'?:'';

                }
                if ($item['certificate_dengji']){
                    $str .= Db::table('system_certificate')->where (['id'=>$item['certificate_dengji']])->value ('certificate_name').'/'?:'';

                }
                if ($item['certificate_zhuanye']){
                    $str .= Db::table('system_certificate')->where (['id'=>$item['certificate_zhuanye']])->value ('certificate_name').'、'?:'';
                }
            }

            $datum['other_certificate'] = $str;
            //分配时间
//            $datum['allocation_date'] = Db::table('clue_person_allocation_record')->where(['operate' => message::$SUCC_PERSON_ALLOCATION,'type'=>$type])->order('create_date DESC')->value('create_date') ?: '';

            //沟通表
            $communicateData = Db::table('clue_communicate_record')
                ->where(['clue_p_id' => $datum['id'],'type' => $type])
                ->whereNull ('delete_time')
                ->order('communicate_date DESC')
                ->field('communicate_date,communicate_content,intention,call')
                ->find ();

            if ($communicateData){
                //最新沟通时间
                if ($communicateData['communicate_date'] === '0000-00-00 00:00:00'){
                    $datum['communicate_date'] = '';
                }else{
                    $datum['communicate_date'] = $communicateData['communicate_date'];
                }
                //沟通记录
                $datum['communicate_content'] = $communicateData['communicate_content'];
                //意向结果
                if ($communicateData['intention']){
                    $datum['intention'] = stateData::$_intention[$communicateData['intention']];
                }else{
                    $datum['intention'] = '';
                }
                //呼叫结果
                if ($communicateData['call']){
                    $datum['call'] = stateData::$_call[$communicateData['call']];
                }else{
                    $datum['call'] = '';
                }
            }else{
                //最新沟通时间
                $datum['communicate_date'] = '';
                //沟通记录
                $datum['communicate_content'] = '';
                //意向结果
                $datum['intention'] = '';
                    //呼叫结果
                $datum['call'] = '';
            }


            if (isset($datum['f_uid'])) {

                //下次沟通时间
                $datum['next_communicate_date'] = Db::table('work_plan')
                    ->where(['custom_name'=>$datum['person_name'],'create_uid' => $datum['f_uid'], 'is_complete' => 2, 'type' => 1])
                    ->whereNull ('delete_time')
                    ->order('create_date DESC')
                    ->value('plan_date') ?: '';

                if ($datum['next_communicate_date'] === '0000-00-00 00:00:00'){
                    $datum['next_communicate_date'] = '';
                }

                //订单个数
                $datum['order_goods_number'] = $this->getPersonOrder($datum['person_id_card'], $uid);
//                $datum['order_goods_number'] = 0;
                //负责人
                $datum['username'] = Db::table('user')->where(['id' => $datum['f_uid']])->value('nickname')?:'';
            }


            if (isset($datum['certificate_code'])){
                $dingshiqi = Db::table('user_message')->where (['b_id'=>$datum['certificate_code'],'is_executed'=>2,'type'=>7])->value ('id');
                if ($dingshiqi){
                    $datum['dingshiqi'] = 1;
                }else{
                    $datum['dingshiqi'] = 0;
                }
            }

        }
        return $data;
    }


    //企业表格数据处理
    public function enterData($data, $uid, $type = 2)
    {
        if (empty($data)) {
            return [];
        }
        //如果是一维数组 强制转化为二维数组
//        if(is_array($data) && count($data) == count($data, COUNT_RECURSIVE)){
        if (is_array($data) && !isset($data[0])) {
            $re = $data;
            unset($data);
            $data[] = $re;
        }
        $cer = new Certificate();
        $deal = new OrderDeal();
        $dzModel = new OrderEnterpriseExchange();

        foreach ($data as &$datum) {

            //获取联系方式
            $datum['mobile'] = Db::table('clue_mobile')->where(['clue_p_id' => $datum['id'],'type'=>$type])->order('create_date DESC')->value('mobile') ?: '';
            $datum['name'] = Db::table('clue_mobile')->where(['clue_p_id' => $datum['id'],'type'=>$type])->order('create_date DESC')->value('name') ?: '';

            //分配时间
//            $datum['allocation_date'] = Db::table('clue_person_allocation_record')->where(['operate' => message::$SUCC_PERSON_ALLOCATION,'type'=>$type])->order('create_date DESC')->value('create_date') ?: '';

            //沟通表
            $communicateData = Db::table('clue_communicate_record')->where(['clue_p_id' => $datum['id'],'type' => $type])->order('communicate_date DESC')->find ();
            if ($communicateData){
                //最新沟通时间
                $datum['communicate_date'] = $communicateData['communicate_date'];
                //沟通记录
                $datum['communicate_content'] = $communicateData['communicate_content'];
                //意向结果
                if ($communicateData['intention']){
                    $datum['intention'] = stateData::$_intention[$communicateData['intention']];
                }else{
                    $datum['intention'] = '';
                }
                //呼叫结果
                if ($communicateData['call']){
                    $datum['call'] = stateData::$_call[$communicateData['call']];
                }else{
                    $datum['call'] = '';
                }
            }else{
                //最新沟通时间
                $datum['communicate_date'] = '';
                //沟通记录
                $datum['communicate_content'] = '';
                //意向结果
                $datum['intention'] = '';
                //呼叫结果
                $datum['call'] = '';
            }



            if (isset($datum['f_uid'])) {
                //下次沟通时间
                $datum['communicate_next'] = Db::table('work_plan')->where(['custom_name'=>$datum['enterprise_name'],'create_uid' => $datum['f_uid'], 'is_complete' => 2, 'type' => 1])->whereNull ('delete_time')->order('create_date DESC')->value('plan_date') ?: '';

                if ($datum['communicate_next'] === '0000-00-00 00:00:00'){
                    $datum['communicate_next'] = '';
                }

                //订单个数
                $datum['order_goods_number'] = $this->getEnterOrder($datum['enterprise_name'], $uid);
                //临时用来改变合作状态
                if ($datum['order_goods_number'] > 0){
                    $datum['status'] = [
                        'id'=>2,
                        'text'=>stateData::$_enter_status[2]
                    ];
                }

                //负责人
                $datum['username'] = Db::table('user')->where(['id' => $datum['f_uid']])->value('nickname')?:'';
            }

        }
        return $data;
    }

    /**
     * 获取该人才的订单个数
     */
    public function getPersonOrder($idcard, $uid)
    {
        $person_count = 0;
        $people_count = 0;

        //查询该人才与企业证书注册订单个数
        $person_count += Db::table('order_enterprise_company')
            ->whereRaw('person_id_card ="' .$idcard.'" and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull ('delete_date')
            ->count();
        //人才倒证
        $person_count += Db::table('order_enterprise_exchange')
            ->whereRaw('id_card ="' .$idcard.'" and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        //人才证书办理
//        $person_count += Db::table('order_other_certificate_business')->where(['person_id_card' => $idcard, 'status' => 1])->count();
        $person_count += Db::table('order_other_certificate_business')
            ->whereRaw('person_id_card ="' .$idcard.'" and (status = 1 OR status = 2 )')
            ->whereNull('delete_date')
            ->count();

        //查询个人提交的人才订单个数
        $people_count += Db::table('order_enterprise_company')
            ->whereRaw('person_id_card ="' .$idcard.'" and get_uid='.$uid.' and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
//print_r ($person_count);die;
        //人才倒证
        $people_count += Db::table('order_enterprise_exchange')
            ->whereRaw('id_card ="' .$idcard.'" and get_uid='.$uid.' and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        //人才证书办理
        $people_count += Db::table('order_other_certificate_business')
            ->whereRaw('person_id_card ="' .$idcard.'" and get_uid = '.$uid.' and (status = 1 OR status = 2 )')
            ->whereNull('delete_date')
            ->count();

        return $people_count . '/' . $person_count;
    }

    /**
     * 获取企业订单个数
     * @param $name
     * @param $uid
     * @return string
     * @throws \think\db\exception\DbException
     */
    public function getEnterOrder($name, $uid)
    {
        //查询该企业证书注册订单个数
//        $person_count = Db::table('order_enterprise_company')->where(['employ_unit_title' => $name, 'status' => 1])->count();
        $person_count = Db::table('order_enterprise_company')
            ->whereRaw('employ_unit_title ="'.$name.'" and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        //倒证
        $person_count += Db::table('order_enterprise_exchange')
            ->whereRaw('employ_unit_title ="'.$name.'" and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        $person_count += Db::table('order_enterprise_exchange')
            ->whereRaw('unit_reg_title ="'.$name.'" and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        $person_count += Db::table('order_enterprise_exchange')
            ->whereRaw('out_unit ="'.$name.'" and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();

        //企业证书
//        $person_count += Db::table('order_enterprise_certificate_business')->where(['enterprise_name' => $name, 'status' => 1])->count();
        $person_count += Db::table('order_enterprise_certificate_business')
            ->whereRaw ('enterprise_name= "'.$name.'" and (status=1 OR status =2)')
            ->whereNull('delete_date')
            ->count();

        //查询个人提交的人才订单个数
//        $people_count = Db::table('order_enterprise_company')->where(['employ_unit_title' => $name, 'status' => 1, 'get_uid' => $uid])->count();
        $people_count = Db::table('order_enterprise_company')
            ->whereRaw('employ_unit_title ="'.$name.'" and get_uid= '.$uid.' and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        //人才倒证
        $people_count += Db::table('order_enterprise_exchange')
            ->whereRaw('employ_unit_title ="'.$name.'" and get_uid= '.$uid.' and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        $people_count += Db::table('order_enterprise_exchange')
            ->whereRaw('unit_reg_title ="'.$name.'"and  get_uid= '.$uid.' and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();
        $people_count += Db::table('order_enterprise_exchange')
            ->whereRaw('out_unit ="'.$name.'" and get_uid= '.$uid.' and (status = 1 OR status = 2 OR status = 3 OR status = 4 OR status = 5)')
            ->whereNull('delete_date')
            ->count();

        //人才证书办理
        $people_count += Db::table('order_enterprise_certificate_business')
            ->whereRaw ('enterprise_name= "'.$name.'" and get_uid='.$uid.' and (status=1 OR status =2)')
            ->whereNull('delete_date')
            ->count();

        return $people_count . '/' . $person_count;
    }


}